home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 489 < prev    next >
Internet Message Format  |  1996-08-06  |  8KB

  1. Path: solon.com!not-for-mail
  2. From: seebs@solutions.solon.com (Peter Seebach)
  3. Newsgroups: gnu.misc.discuss,comp.std.c
  4. Subject: Re: Coding Standards are ignorant
  5. Date: 3 Mar 1996 19:37:26 -0600
  6. Organization: Usenet Fact Police (Undercover)
  7. Message-ID: <4hdhgm$6vq@solutions.solon.com>
  8. References: <4gum82$14v4@info4.rus.uni-stuttgart.de> <1996Mar309.45.57.5707@koobera.math.uic.edu> <4hc8os$qs@solutions.solon.com> <1996Mar319.58.13.6548@koobera.math.uic.edu>
  9. NNTP-Posting-Host: solutions.solon.com
  10.  
  11. [Note newsgroups.  Please restrict crossposted followups to those relevant
  12.  to both groups. -sbs]
  13.  
  14. [For the new crowd; I have been attempting to debate whether or not:
  15.  1.  "C" and "ANSI C" are significantly different things, in particular,
  16.   whether or not portability to pre-ANSI compilers is a desirable feature.
  17.  
  18.  2.  Whether or not code which looks like
  19. >static char ok[256] = {
  20. > 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  21. >,0,7,0,7,7,7,7,7,0,0,7,7,7,7,7,7 ,7,7,7,7,7,7,7,7,7,7,0,0,0,7,0,7
  22. >,0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 ,7,7,7,7,7,7,7,7,7,7,7,0,0,0,7,7
  23. >,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 ,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,0
  24. >,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  25. >,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  26. >,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  27. >,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  28. >} ;
  29. >
  30. >for (i = 0;s[i];++i) if (ok[(unsigned int) (unsigned char) s[i]]) return 1;
  31.   is good style.
  32.  
  33.  3.  Whether or not declaring main to return void is, as I claim, gratuitously
  34.   stupid and entirely benefit free.
  35.  
  36.  I apologize in advance for any resulting flames; I have come to the
  37.  conclusion that Dan B. does not consider me a reliable and experienced
  38.  source, and humbly offer the debate to the eyes of those wiser and more
  39.  experienced to discuss. -s]
  40.  
  41. In article <1996Mar319.58.13.6548@koobera.math.uic.edu>,
  42. D. J. Bernstein <djb@koobera.math.uic.edu> wrote:
  43. >Peter Seebach <seebs@solutions.solon.com> wrote:
  44. >> I have no problem with a program called a "compiler for a language similar
  45. >> to C" which does not compile C.
  46.  
  47. >pcc is a C compiler.
  48.  
  49. No, it isn't.  It compiles a language which is different from the language
  50. documented in ISO 9899-1990.  You might as well claim a yardstick is
  51. a meter long, because it's pretty close, and would have been accepted
  52. by most people in the absence of a standard.
  53.  
  54. >That didn't change when ANSI C was published.
  55.  
  56. Yes, it did.  In the absence of a standard, a good approximation is good
  57. enough.  In the presence of a standard, it isn't.  For thousands of
  58. years, anything within a certain range was "a cubit" or "a foot".  A foot
  59. is now quite precisely defined.  You can say something is "about a foot
  60. long" or "about a meter long", but to say it is *exactly* a foot long
  61. requires that it adhere to the standard.  It didn't before there was
  62. a standard.
  63.  
  64. >Of course, pcc is not an ANSI C compiler, but if you persist in equating
  65. >``C'' with ``ANSI C'' you will continue making false statements.
  66.  
  67. C is now ANSI/ISO C.  There are related languages, including many ancestors.
  68. Should we count BCPL as a kind of C, too?  Or B?  After all, both have
  69. a claim to being basically the same language.
  70.  
  71. >> > > Bad style.  Magic numbers should *never* be used.  Period.
  72. >> > More religious fanaticism. Wonderful.
  73. >> Care to offer an example of any case in which a magic number is good
  74. >> coding style?
  75.  
  76. >The canonical example of magic numbers is the table of constants in a
  77. >CRC algorithm. Another example is Terje Mathisen's f_magic, i_magic
  78. >pair. Calling this ``bad style,'' and saying that it should ``*never* be
  79. >used,'' reflects poorly on your experience.
  80.  
  81. Those are a different kind of magic numbers.  Using an arbitrary value to
  82. contrast with 0 in a table is bad style.  Using a specific set of numbers
  83. because *those particular numbers work differently* is quite different.
  84. In case you hadn't noticed, English is heavily overloaded in the area
  85. of jargon.  Look it up; I'm talking magic number [1] in jargon - you're
  86. then using magic number [2] and claiming it's bad style.
  87.  
  88. (Technically, yours doesn't even have the benefits of magic number [1],
  89. namely, it *doesn't* matter that you used 7 instead of 1 as the "non-zero
  90. value" for an if clause; this is arguably worse style, but certainly
  91. no better.)
  92.  
  93. >Another very useful example is the 50462976 trick---
  94.  
  95. >   static uint32 tr[4] = { 50462976, 117835012, 185207048, 252579084 } ;
  96. >   #define ctr ((unsigned char *) tr)
  97.  
  98. >---where I think being flashy for the novice reader is much more fun
  99. >than writing the constants in the most obvious way. (I'd _love_ to see
  100. >your ``portable'' version of this code, btw.)
  101.  
  102. Well, to start with, I wouldn't have used a byte-order dependant form.
  103. I'd probably do
  104.     static unsigned char ctr[16] = {
  105.         0x00, 0x01, 0x02, 0x03,
  106.         0x04, 0x05, 0x06, 0x06,
  107.         0x08, 0x09, 0x0A, 0x0B,
  108.         0x0C, 0x0D, 0x0E, 0x0F,
  109.     };
  110. and my code would work similarly, except that someone unfamiliar with
  111. the code could tell exactly what I meant it to do.  Additionally, my
  112. code would work on machines of arbitrary byte order or byte size.
  113.  
  114. >> Fascinating; how, precisely, is it not a magic number?
  115.  
  116. >Easy: there's nothing magic about it. Any other number (except multiples
  117. >of 256) would have the same effect.
  118.  
  119. Well, in that case, we're back to the old style point of not putting in
  120. extra information that has no meaning.  One traditionally favors 0 for
  121. false and 1 for true, *when producing values*.  (Only fools do things
  122. like
  123.     if (isalpha(c) == 1)
  124. ...)
  125.  
  126. If you produce other values, or use them, it is implicit that there's a reason
  127. for it; if this reason is undocumented, you have introduced a magic number
  128. [1].  If there is no reason, you have wasted a future maintainer's time.
  129.  
  130. >Very good. Now you understand why I only gave you _part_ of the test: I
  131. >wanted you to focus on what was happening inside the code, without being
  132. >distracted by its purpose.
  133.  
  134. Focussing on what is happening, without looking at the purpose, is a good
  135. way to discover a microoptimization that lets a redundant or useless test
  136. occur in 20% less time.
  137.  
  138. >> The poor sod has to search for references to the table
  139. >> and scrounge around trying to figure out what meaning, if any, the 7 had.
  140.  
  141. >The production version of that code is a dinky little (77-line) quote.c,
  142. >where the ``ok'' table is declared static and used exactly once. Not too
  143. >hard to figure out.
  144.  
  145. Except that even then, they have to study the usage to determine that,
  146. apparently, it's just "non-zero" that's relevant.  If you use 0's and 1's,
  147. it is obvious to any reader instantly what you plan to do; if you use
  148. 0's and 7's, you are indicating that something is probably up.
  149.  
  150. [re: void main]
  151.  
  152. >> Okay; how about this.  No *significant* benefit.  And a significant
  153. >> loss.
  154.  
  155. >Depends on what's ``significant'' to the programmer. Anyway, I take it
  156. >you're no longer claiming ``NO BENEFIT WHATSOEVER''?
  157.  
  158. I still maintain that there is no *benefit*.  You may find it convenient
  159. or pleasureable to declare main to return void; so?  Rats will self-administer
  160. pleasure center shocks until they starve.  For it to be a benefit, it must
  161. provide an enhancement to the code.  The illusion of being able to fall
  162. off the end of main without consequences is hardly a benefit.
  163.  
  164. [re: the "mistakes" I make in ignoring pre-ANSI bogosities, like the redundant
  165.  cast to (unsigned int) in the example code above.]
  166. >> I am *by definition* not making mistakes.
  167.  
  168. >Good .sig fodder.
  169.  
  170. It certainly is.  Other ones you may find relevant:
  171.     "This is (no offense meant) just plain stupid."
  172.  
  173. -s
  174. -- 
  175. Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
  176. C/Unix wizard -- C/Unix questions? Send mail for help.  No, really!
  177. FUCK the communications decency act.  Goddamned government.  [literally.]
  178. The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
  179.